package com.lq.educationsystem.lqcourse.service.impl;

import com.lq.educationsystem.common.util.*;
import com.lq.educationsystem.common.vo.CoursePageVo;
import com.lq.educationsystem.lqcourse.entiy.CourseTb;
import com.lq.educationsystem.lqcourse.mapper.CourseTbMapper;
import com.lq.educationsystem.lqcourse.service.CourseBaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseBaseImpl implements CourseBaseService {
    @Autowired
    CourseTbMapper courseTbMapper;
    @Override
    public void insertInfo(CourseTb courseInfo) {
        int row = courseTbMapper.insertSelective(courseInfo);
        System.out.println("Course插入影响的行数: "+row);
    }

    @Override
    public void deleteById(Integer courseId) {
        int row = courseTbMapper.deleteByPrimaryKey(Long.valueOf(courseId));
        System.out.println("Course删除影响的行数: "+row);
    }

    @Override
    public void updateInfo(CourseTb courseInfo) {
        int row = courseTbMapper.updateByPrimaryKeySelective(courseInfo);
        System.out.println("Course更新影响的行数: "+row);
    }

    @Override
    public Result<CourseTb> queryById(Integer courseId) {
        CourseTb courseInfo = courseTbMapper.selectByPrimaryKey(Long.valueOf(courseId));
        return ResultBuild.buildOk(new Result<CourseTb>(),courseInfo);
    }

    @Override
    public ResultPage<CourseTb> queryList(CoursePageVo coursePageVo) {
        Integer page = coursePageVo.getPage();
        Integer pageSize = coursePageVo.getPageSize();

        //获取起始页面
        int start = PageUtil.getStart(page, pageSize);
        //数据
        CourseTb courseTb = new CourseTb();

        BeanUtils.copyProperties(coursePageVo,courseTb);

        //调用mapper
        List<CourseTb> courseTbs= courseTbMapper.queryList(start,pageSize,courseTb);
        int counts=0;
        //TODO： 查询数据总数还没有做
        return ResultPageBuild.pageBuildOk(new ResultPage<CourseTb>(),page,pageSize,counts,courseTbs);
    }
}
